﻿
/*=========================================
* Author: Administrator
* DateTime:2017/6/20 19:21:23
* Description:$safeprojectname$
==========================================*/

using System;
using System.Collections.Generic;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using UnityEngine;

namespace OrderSystem
{
    internal class WaiterMediator : Mediator
    {
        private WaiterProxy waiterProxy = null;
        public new const string NAME = "WaiterMediator";
        public WaiterView WaiterView
        {
            get { return (WaiterView) base.ViewComponent; }
        }

        //todo 订单代理
        private OrderProxy orderProxy = null;

        public WaiterMediator( WaiterView view ) : base(NAME, view)
        {

            WaiterView.SendMenu += (WaiterItem item) =>
            {
                SendNotification(OrderSystemEvent.UPMENU, item.serverOrder);
               
            };

           WaiterView.SendFood += (WaiterItem item) => {
               Debug.Log("取餐成功");
               SendNotification(OrderSystemEvent.SEND_FOOD_TO_CLIENT, item);
               
           };          
        }

        public override void OnRegister( )
        {
            base.OnRegister();
            waiterProxy = Facade.RetrieveProxy(WaiterProxy.NAME) as WaiterProxy;
            orderProxy = Facade.RetrieveProxy(OrderProxy.NAME) as OrderProxy;
            if ( null == waiterProxy )
                throw new Exception(WaiterProxy.NAME + "is null,please check it!");
            if ( null == orderProxy )
                throw new Exception(OrderProxy.NAME + "is null,please check it!");
            WaiterView.UpdateWaiter(waiterProxy.Waiters);

        }

        public override IList<string> ListNotificationInterests( )
        {
            IList<string> notifications = new List<string>();
            notifications.Add(OrderSystemEvent.FOOD_TO_CLIENT);
            notifications.Add(OrderSystemEvent.UPDATE_WAITER_VIEW);
            notifications.Add(OrderSystemEvent.GET_PAY);
            notifications.Add(OrderSystemEvent.WAIT_SEND_FOOD);
            notifications.Add(OrderSystemEvent.UPDATE_WAITER_WAIT_CLIENT_HELLO);
            return notifications;
        }
        public override void HandleNotification( INotification notification )
        {
            switch (notification.Name)
            {
                case OrderSystemEvent.UPDATE_WAITER_WAIT_CLIENT_HELLO://等待客户招呼,结账
                    WaiterView.UpdateWaiter(notification.Body as IList<WaiterItem>);
                    break;
                case OrderSystemEvent.WAIT_SEND_FOOD://等待送餐
                    WaiterView.UpdateWaiter(notification.Body as IList<WaiterItem>);
                    break;
                case OrderSystemEvent.FOOD_TO_CLIENT://送餐
                    Debug.Log(" 服务员取菜 ");
                    WaiterView.UpdateWaiter(notification.Body as IList<WaiterItem>);
                    Debug.Log("服务员取到餐了，开始送餐给客人");                
                    break;
                case OrderSystemEvent.UPDATE_WAITER_VIEW:
                    WaiterView.UpdateWaiter(notification.Body as IList<WaiterItem>);
                    break;
                case OrderSystemEvent.GET_PAY:
                   //服务员收到付款进入休息状态。
                    WaiterView.UpdateWaiter(notification.Body as IList<WaiterItem>);
                    break;

            }
        }
        private WaiterItem GetIdleWaiter()
        {
            foreach (WaiterItem waiter in waiterProxy.Waiters)
                if ( waiter.state.Equals(E_WaiterState.Idle))
                    return waiter;
            Debug.LogWarning("暂无空闲服务员请稍等..");
            return null;
        }
    }
}